//
//  OilCityViewController.m
//  CarCare
//
//  Created by ileo on 15/4/2.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "OilCityViewController.h"
#import "SetOilPriceViewController.h"
#import "NetCity.h"
#import "FuelMgr.h"
typedef NS_ENUM(NSInteger , View_Type) {
    View_Type_LeftL,//左侧label
    View_Type_RightImageV,//右侧imageV
};

#define CELL_HEIGHT 44

@interface OilCityViewController()<UITableViewDataSource, UITableViewDelegate,UIViewControllerBackButtonDataSource>

@property (nonatomic, copy) NSArray *citys;

@property (nonatomic , strong) NSIndexPath * selectIndexPath;


@end

@implementation OilCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)fillCitys:(NSArray *)citys{
    self.citys = citys;
    [self.tableView reloadData];
}

#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.citys.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELL_HEIGHT;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        CGFloat labelX = 20 * __MAIN_RATIO_375;
        [[[UIView viewWithFrame:CGRectMake(labelX, 0.5, __MAIN_WIDTH - labelX * 2, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell];
        [[[UIView viewWithFrame:CGRectMake(labelX, CELL_HEIGHT, __MAIN_WIDTH - labelX * 2, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell];
        UIImage * rightImage = ImageNamed(@"choiceOilCellImage");
        UIImageView * rightImageV = [Factory imageViewWithCenter:CGPointMake(__MAIN_WIDTH - labelX - rightImage.size.width / 2, CELL_HEIGHT / 2) image:rightImage onView:cell];
        rightImageV.tag = View_Type_RightImageV;
        [rightImageV setHidden:YES];
        UILabel * leftL = [Factory labelWithFrame:CGRectMake(labelX, 0, __MAIN_WIDTH - labelX * 2 - rightImage.size.width, CELL_HEIGHT) font:FONT_DEFAULT_Light(15) text:@"" textColor:COLOR_HEXSTRING(@"#424242") onView:cell textAlignment:NSTextAlignmentLeft];
        leftL.backgroundColor = [UIColor clearColor];
        leftL.tag = View_Type_LeftL;

    }

    CityModel *model = self.citys[indexPath.row];
    
    UILabel * label = (UILabel *)[cell viewWithTag:View_Type_LeftL];
    label.text = model.name;
    UIImageView * choiceImageV = (UIImageView *)[cell viewWithTag:View_Type_RightImageV];
    
    if([[FuelMgr sharedInstance].choiceProvince isEqualToString:model.name] || [[FuelMgr sharedInstance].choiceCity isEqualToString:model.name])
    {
        [label setTextColor:[Theme mainColor]];
        [choiceImageV setHidden:NO];
    }
    
    [label setTextColor:((self.selectIndexPath && indexPath.row == self.selectIndexPath.row && indexPath.section == self.selectIndexPath.section) ? [Theme mainColor] : COLOR_HEXSTRING(@"#424242"))];
    [choiceImageV setHidden:((self.selectIndexPath && indexPath.row == self.selectIndexPath.row && indexPath.section == self.selectIndexPath.section) ? NO : YES)];
    
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectIndexPath = indexPath;
    
    CityModel *model = self.citys[indexPath.row];
    if(model.municipality && model.level != 2)
    {
        [FuelMgr sharedInstance].choiceProvince = model.name;
        [FuelMgr sharedInstance].choiceCity = model.name;
    }else if (model.level != 2) {
        [FuelMgr sharedInstance].choiceProvince = model.name;
    }else
    {
        [FuelMgr sharedInstance].choiceCity = model.name;
    }
    if (model.municipality || model.level == 2) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        __weak __typeof(self) wself = self;
        [[NetCity citysWithProvicePID:model.pid] requestSuccess:^(id JSON) {
            NSArray *tmp = JSON[@"infolist"];
            NSMutableArray *arr = [NSMutableArray arrayWithCapacity:tmp.count];
            for (int i = 0; i < tmp.count; i++) {
                [arr addObject:[[CityModel alloc] initWithDic:tmp[i]]];
            }
            OilCityViewController *vc = [[[OilCityViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
            vc.title = @"选择城市";
            [vc fillCitys:arr];
            
            [wself.navigationController pushViewController:vc animated:YES];
        }];
    }
    [self.tableView reloadData];
}

-(void)navClickOnBackItem{
    CityModel *model = self.citys[self.selectIndexPath.row];
    if (model.level == 1) {
        [[FuelMgr sharedInstance]cancelResetDefault];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
